//
//  DYAccountProxy.m
//  dyss_v2
//
//  Created by LXF on 16/1/28.
//  Copyright © 2016年 First Facility. All rights reserved.
//

#import "DYAccountProxy.h"
#import "CustomLogTools.h"
#import "DYNetworkDelegate.h"
#import "DYAPI.h"

#undef Def_DYNotificationKey
#define Def_DYNotificationKey(key) DYNotificationKey key = @"DYNotificationKey." #key;

SMSTYPE_Type kSMSTYPE_Type_Forget = @"f";
SMSTYPE_Type kSMSTYPE_Type_Register = @"r";

Def_DYNotificationKey(kNC_Login)
Def_DYNotificationKey(kNC_Logout)

#define kCurrentyAccountInfo @"DYAppDelegate.kCurrentyAccountInfo"
static DYAccountProxy *curreAccountProxy = nil;

@implementation DYAccountProxy

proxyImplementation(DYAccount);

+(instancetype)accountProxyWithAccount:(NSString *)account password:(NSString *)password{
    DYAccount *r = [[DYAccount alloc]init];
    r.account = account;
    r.password = password;
    id ap = [self mpWithModel:r];
    return ap;
}

-(void)setCurreAccountProxy{
    if (self.model.identifier) {
        NSString *string = [self.model toJSONString];
        [[NSUserDefaults standardUserDefaults]setObject:string forKey:kCurrentyAccountInfo];
        curreAccountProxy = self;
        [[NSNotificationCenter defaultCenter]postNotificationName:kNC_Login object:nil userInfo:@{@"account":curreAccountProxy}];
    }else{
        printE(@"请不要将空值设置为当前 用户");
    }
}

+ (DYAccountProxy *)isLogged{
    if (curreAccountProxy) {
        return curreAccountProxy;
    }
    NSString *str = [[NSUserDefaults standardUserDefaults]objectForKey:kCurrentyAccountInfo];
    if (str) {
        DYAccountProxy *mp = [DYAccountProxy mpWithModelJson:str];
        if (mp) {
            curreAccountProxy = mp;
            return mp;
        }
    }
    return nil;
}

- (void)logout:(void(^)(NSError *error))finished{
    if (curreAccountProxy) {
        curreAccountProxy = nil;
        [[NSUserDefaults standardUserDefaults]removeObjectForKey:kCurrentyAccountInfo];
        finished(nil);
        [[NSNotificationCenter defaultCenter]postNotificationName:kNC_Logout object:nil userInfo:@{}];
        return;
    }
    finished(nil);
    [[NSNotificationCenter defaultCenter]postNotificationName:kNC_Logout object:nil userInfo:@{}];
}



-(void)loginSuccess:(void (^)(NSURLSessionDataTask *, id, id))success dataerr:(void (^)(NSURLSessionDataTask *, id, XXError *))dataerr failure:(void (^)(NSURLSessionDataTask *, NSError *))failure{
    
    DYNetworkDelegate *delegate = [[DYNetworkDelegate alloc]init];
    
    [delegate encryption2WithKeyByPassword];
    
    [delegate autoToAny:[DYAccountProxy class] keyPath:@"data.account"];
//    [delegate autoToAnyList:[DYAccountProxy class] keyPath:@"data.list"];

    NSDictionary *parames = @{
                              @"account": self.model.account ?: @"",
                              @"password": self.model.password ?: @"",
                              };
    
    [[DYAPI shared] doTaskUrlInfo:[DYURLSet shared].login parameters:parames netdelegate:delegate success:success dataerr:dataerr failure:failure];
}

-(void)registWithAuthCode:(NSString *)authCode Success:(void (^)(NSURLSessionDataTask *task, id responseObject,id target))success
                  dataerr:(void (^)(NSURLSessionDataTask *task, id responseObject,XXError *error))dataerr
                  failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure{
    DYNetworkDelegate *delegate = [[DYNetworkDelegate alloc]init];
    
    // 自动对 key = password 的参数加密 {第一次加密}
    [delegate encryption1WithKeyByPassword];
    
    NSDictionary *parames = @{
                              @"password": self.model.password,
                              @"authCode": authCode,
                              };
    
    doTaskUrlInfo(regist)
    
}

+(void)forgetWithPassword:(NSString *)password authCode:(NSString *)authCode success:(void (^)(NSURLSessionDataTask *, id, id))success dataerr:(void (^)(NSURLSessionDataTask *, id, XXError *))dataerr failure:(void (^)(NSURLSessionDataTask *, NSError *))failure{
    
    DYNetworkDelegate *delegate = [[DYNetworkDelegate alloc]init];
    
    // 自动对 key = password 的参数加密 {第一次加密}
    [delegate encryption1WithKeyByPassword];
    
    NSDictionary *parames = @{
                              @"password": password,
                              @"authCode": authCode,
                              };
    
    doTaskUrlInfo(forget)
    // ⬆️ 等价 ⬇️
    //[[DYAPI shared] doTaskUrlInfo:[DYURLSet shared].forget parameters:parames netdelegate:delegate success:success dataerr:dataerr failure:failure];
    
}

+(void)smscWithType:(SMSTYPE_Type)type
                tel:(NSString *)tel
            success:(void (^)(NSURLSessionDataTask *task, id responseObject,id target))success
            dataerr:(void (^)(NSURLSessionDataTask *task, id responseObject,XXError *error))dataerr
            failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure{
    
    DYNetworkDelegate *delegate = [[DYNetworkDelegate alloc]init];
    
    NSDictionary *parames = @{
                              @"type": type,
                              @"tel": tel,
                              };
    
    doTaskUrlInfo(smsc)
}

+ (void)changePasswordWithAccountId:(NSString *)accountId password:(NSString *)password newPassword:(NSString *)newPassword success:(void (^)(NSURLSessionDataTask *, id, id))success dataerr:(void (^)(NSURLSessionDataTask *, id, XXError *))dataerr failure:(void (^)(NSURLSessionDataTask *, NSError *))failure {
    
    DYNetworkDelegate *delegate = [[DYNetworkDelegate alloc]init];
    
    NSDictionary *parames = @{
                              @"id": accountId,
                              @"password": password,
                              @"newPassword": newPassword
                              };
    
    [delegate encryption1WithKeys:@[@"password",@"newPassword"]];
    
    doTaskUrlInfo(changePassword)
}

+(void)changeNickNameWithAccountId:(NSString *)accountId
                          nickName:(NSString *)nickName
                           success:(void (^)(NSURLSessionDataTask *task, id responseObject,id target))success
                           dataerr:(void (^)(NSURLSessionDataTask *task, id responseObject,XXError *error))dataerr
                           failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure {
    DYNetworkDelegate *delegate = [[DYNetworkDelegate alloc]init];
    
    NSDictionary *parames = @{
                              @"id": accountId,
                              @"nickname": nickName,
                              };
        
    doTaskUrlInfo(changeNickName)
}
@end
